﻿namespace NetBrother.TopShow
{
    using IBatisNet.DataMapper;
    using System.Data.SQLite;

    public class MyBatisDaoBase<T> where T : class
    {
        private ISqlMapper _reader;
        private ISqlMapper _writer;
        public ISqlMapper SqlMapper { get; set; }

        //protected static log4net.ILog _log = log4net.LogManager.GetLogger("MyBatisDaoBase");


        public MyBatisDaoBase(string mapperName)
        {
            this.SqlMapper = MyBatisMapperManager.GetMapper(mapperName);
                       
        }
        public MyBatisDaoBase() { }

        public MyBatisDaoBase(System.Uri readerUri, System.Uri writerUri)
        {
            this._reader = MyBatisMapperManager.GetMapper(readerUri);
            this._writer = MyBatisMapperManager.GetMapper(writerUri);
            if (!string.IsNullOrEmpty(_reader.DataSource.DbProvider.Description))
            {
                if (_reader.DataSource.DbProvider.Description.IndexOf("SQLite") > 0)
                {
                    string pluginServicePath = PluginService.DB.Util.GetWindowsServiceInstallPath("PluginService");
                    var path = pluginServicePath.Replace("PluginService.exe", "");
                    string sqliteDbName = "FaceDeteiveDB.db";
                    var connstring = string.Format("Data Source={0}{1};Version={2};", path, sqliteDbName, 3);
                    this._reader.DataSource.ConnectionString = connstring;
                    _writer.DataSource.ConnectionString = connstring;
                }

                if (_writer.DataSource.DbProvider.Description.IndexOf("SQLite") > 0)
                {
                    string pluginServicePath = PluginService.DB.Util.GetWindowsServiceInstallPath("PluginService");
                    var path = pluginServicePath.Replace("PluginService.exe", "");
                    string sqliteDbName = "FaceDeteiveDB.db";
                    var connstring = string.Format("Data Source={0}{1};Version={2};", path, sqliteDbName, 3);
                    this._reader.DataSource.ConnectionString = connstring;
                    _writer.DataSource.ConnectionString = connstring;
                }

            }
        }

        public MyBatisDaoBase(string readerName, string writerName)
        {
            
            this._reader = MyBatisMapperManager.GetMapper(readerName);
            this._writer = MyBatisMapperManager.GetMapper(writerName);
            if (!string.IsNullOrEmpty(_reader.DataSource.DbProvider.Description))
            {
                if (_reader.DataSource.DbProvider.Description.IndexOf("SQLite") > 0)
                {
                    string pluginServicePath = PluginService.DB.Util.GetWindowsServiceInstallPath("PluginService");
                    var path = pluginServicePath.Replace("PluginService.exe", "");
                    string sqliteDbName = "FaceDeteiveDB.db";
                    var connstring = string.Format("Data Source={0}{1};Version={2};", path, sqliteDbName, 3);
                    this._reader.DataSource.ConnectionString = connstring;
                    _writer.DataSource.ConnectionString = connstring;
                }

                if (_writer.DataSource.DbProvider.Description.IndexOf("SQLite") > 0)
                {
                    string pluginServicePath = PluginService.DB.Util.GetWindowsServiceInstallPath("PluginService");
                    var path = pluginServicePath.Replace("PluginService.exe", "");
                    string sqliteDbName = "FaceDeteiveDB.db";
                    var connstring = string.Format("Data Source={0}{1};Version={2};", path, sqliteDbName, 3);
                    this._reader.DataSource.ConnectionString = connstring;
                    _writer.DataSource.ConnectionString = connstring;
                }

            }
            
        }

        public ISqlMapper Writer
        {
            get { return this._writer; }
            set { this._writer = value; }
        }

        public ISqlMapper Reader
        {
            get { return this._reader; }
            set { this._reader = value; }
        }

        protected virtual string GetStatementName(string name)
        {
            //return string.Format("{0}.{1}", typeof(T).Namespace, name);
            return name;
        }

        public string GetRunSql(string Statement, object para)
        {
            var mapper = this.Writer;
            if (!mapper.IsSessionStarted)
            {
                mapper.OpenConnection();
            }
            var statement = mapper.GetMappedStatement(GetStatementName(Statement));
            var scope = statement.Statement.Sql.GetRequestScope(statement, para, mapper.LocalSession);
            return scope.PreparedStatement.PreparedSql;
        }
    }
}

